package chat.service.Impl;

import chat.mapper.ChatRoomMapper;
import chat.mapper.MessageMapper;
import chat.service.AdminServiceFeignClient;
import chat.service.IChatService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import common.model.chat.ChatRoom;
import common.model.chat.Message;
import common.model.result.Result;
import common.model.result.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ChatServiceImpl implements IChatService {

    @Autowired
    ChatRoomMapper chatRoomMapper;

    @Autowired
    MessageMapper messageMapper;

    @Autowired
    AdminServiceFeignClient adminServiceFeignClient;

    @Override
    public void insertChatRoom(ChatRoom chatRoom) {
        chatRoomMapper.insert(chatRoom);
    }

    @Override
    public ChatRoom findActiveChatRoomByUserId(Long userIdLong) {
        return chatRoomMapper.selectOne(new LambdaQueryWrapper<ChatRoom>().eq(ChatRoom::getUserId, userIdLong).eq(ChatRoom::getStatus, 1));
    }

    @Override
    public void updateChatRoom(ChatRoom chatRoom) {
        // 确保只更新具有特定ID的聊天室
        if (chatRoom.getId() != null) {
            System.out.println("adminId"+chatRoom.getAdminId());
            chatRoomMapper.updateById(chatRoom);
        } else {
            // 如果没有ID，可以根据其他唯一标识更新，比如conversationId
            LambdaQueryWrapper<ChatRoom> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ChatRoom::getConversationId, chatRoom.getConversationId());
            chatRoomMapper.update(chatRoom, wrapper);
        }
    }

    @Override
    public Result getOnlineAdminId() {
        return adminServiceFeignClient.getOnlineAdminId();
    }

    @Override
    public boolean isChatRoomCreated(Long userId) {
        ChatRoom chatRoom = chatRoomMapper.selectOne(new LambdaQueryWrapper<ChatRoom>().eq(ChatRoom::getUserId, userId).eq(ChatRoom::getStatus, 1));
        if (chatRoom != null) {
            return true;
        }
        return false;
    }

    @Override
    public List<ChatRoom> getActiveChatRooms() {
        return chatRoomMapper.selectList(new LambdaQueryWrapper<ChatRoom>().eq(ChatRoom::getStatus, 1).eq(ChatRoom::getAdminId, 0));
    }

    @Override
    public void insertMessage(Message newMessage) {
        messageMapper.insert(newMessage);
    }

    @Override
    public ChatRoom getChatRoom(Long fromId) {
        ChatRoom chatRoom = chatRoomMapper.selectOne(new LambdaQueryWrapper<ChatRoom>().eq(ChatRoom::getUserId, fromId).eq(ChatRoom::getStatus, 1));
        System.out.println(chatRoom);
        return chatRoom;
    }

    @Override
    public Result getHistoryMessage(Long userId) {
        ChatRoom chatRoom = chatRoomMapper.selectOne(new LambdaQueryWrapper<ChatRoom>().eq(ChatRoom::getUserId, userId).eq(ChatRoom::getStatus, 1));
        List<Message> messages = new ArrayList<>();
        if (chatRoom != null) {
            messages = messageMapper.selectList(new LambdaQueryWrapper<Message>().eq(Message::getConversationId, chatRoom.getConversationId()));
        }
        return Result.success(messages);
    }

    @Override
    public Result closeChatRoom(Long userId) {
        ChatRoom chatRoom = chatRoomMapper.selectOne(new LambdaQueryWrapper<ChatRoom>().eq(ChatRoom::getUserId, userId).eq(ChatRoom::getStatus, 1));
        if (chatRoom != null) {
            chatRoom.setStatus(2);
            chatRoomMapper.updateById(chatRoom);
            return Result.success();
        } else {
            return Result.failure(ResultCodeEnum.BAD_REQUEST,"无活跃聊天室");
        }

    }

}
